import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:gun/event/inventory_event.dart';
import 'package:gun/page/down_page.dart';
import 'package:gun/page/inventory_page.dart';
import 'package:gun/page/move_page.dart';
import 'package:gun/page/query_page.dart';
import 'package:gun/page/up_page.dart';

import '../main.dart';
import 'setting_common_page.dart';

class Index extends StatefulWidget {
  const Index({Key? key}) : super(key: key);

  @override
  State<Index> createState() => _IndexState();
}

class _IndexState extends State<Index> {


  @override
  initState() {
    super.initState();
    _initUHF();
    const MethodChannel('toFlutter').setMethodCallHandler(flutterMethod);
  }

  Future flutterMethod(MethodCall methodCall) async{
    switch (methodCall.method) {
      case 'data':
        print(methodCall.arguments);
        eventFactory.fire(InventoryEvent(methodCall.arguments));
        break;
    }
  }

  _initUHF() async {
    var result = await const MethodChannel('toAndroid').invokeMethod('initUHF');
    print('init $result');
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/bg.png",
            ),
          ),
        ),
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Positioned(
              top: 100.h,
              child:Image.asset("assets/images/login_title.png",width: 280.w,fit: BoxFit.fitWidth,),
            ),
            Positioned(
              top: 25.h,
              right: 10.w,
              child:InkWell(
                onTap: (){
                  Get.defaultDialog(
                    title: '退出',
                    titleStyle: TextStyle(
                      color: const Color(0xFF000000),
                      fontSize: 16.sp,
                    ),
                    middleText: '确定退出应用？',
                    middleTextStyle: TextStyle(
                      color: const Color(0xFF000000),
                      fontSize: 14.sp,
                    ),
                    radius: 14.sp,
                    textConfirm: '确定',
                    // textCancel: '取消',
                    confirmTextColor: const Color(0xFF007AFF),
                    backgroundColor: const Color(0xD1F8F8F8),
                    buttonColor: Colors.transparent,
                    onConfirm: () {
                      SystemNavigator.pop();
                      exit(0);
                    },
                  );
                },
                child: Image.asset("assets/images/quit.png",width: 60.w,fit: BoxFit.fitWidth,),
              )
            ),
            Positioned(
              top: 25.h,
              right: 90.w,
              child:InkWell(
                onTap: (){
                  Get.to(()=>const SettingCommon());
                },
                child:Image.asset("assets/images/setting.png",width: 60.w,fit: BoxFit.fitWidth,),
              )
            ),
            Positioned(
              top: 25.h,
              left: 10.w,
              child:Text('${settingEntity.deviceName} ',style: const TextStyle(color: Colors.white),)
            ),
            Positioned(
                top: 170.h,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    InkWell(
                      child: Image.asset("assets/images/inventory.png",width: 360.w,fit: BoxFit.fitWidth,),
                      onTap: (){
                        Get.to(()=>const Inventory());
                      },
                    ),
                    SizedBox(height: 14.h,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        InkWell(
                          child: Image.asset("assets/images/up.png",width: 175.w,fit: BoxFit.fitWidth,),
                          onTap: (){
                            Get.to(()=>const Up());
                          },
                        ),
                        SizedBox(width: 10.w,),
                        InkWell(
                          child: Image.asset("assets/images/query.png",width: 175.w,fit: BoxFit.fitWidth,),
                          onTap: (){
                            Get.to(()=>const Query());
                          },
                        ),
                      ],
                    ),
                    SizedBox(height: 14.h,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        InkWell(
                          onTap: (){
                            Get.to(()=>const Down());
                          },
                          child: Image.asset("assets/images/down.png",width: 175.w,fit: BoxFit.fitWidth,),
                        ),
                        SizedBox(width: 10.w,),
                        InkWell(
                          onTap: (){
                            Get.to(()=>const Move());
                          },
                          child: Image.asset("assets/images/move.png",width: 175.w,fit: BoxFit.fitWidth,),
                        ),
                      ],
                    ),
                  ],
                )
            ),
            Positioned(
              bottom: 25.h,
              child: Text('${settingEntity.deviceNo} v2.1.1',style: const TextStyle(color: Colors.white),)
            ),
          ],
        )
      ),
    );
  }
}